# from socket import *
#
#
# class MySocket:
#     def __init__(self, HOST=None, PORT=None):
#         self.sock = self.connect()
#         self.HOST = HOST
#         self.PORT = PORT
#         self.ADDR = (HOST, PORT)
#
#     def connect(self):
#         sock = socket()
#         sock.connect(self.ADDR)
#         return sock
#
#     def main(self):
#         while True:
#             self.sock.send(input('>>').encode())
#             data = self.sock.recv(1024)
#             if not data:
#                 break
#             print(data.decode())
#         self.sock.close()
import sys
from socket import *


class FTPClient:
    def __init__(self):
        self.ADDR = ('176.17.13.23', 1210)
        self.sock = self.connect()

    def connect(self):
        sock = socket()
        sock.connect(self.ADDR)
        return sock

    def exit(self):
        self.sock.send(b'EXIT')
        self.sock.close()
        sys.exit('谢谢使用')

    def view(self):
        self.sock.send(b'VIEW')
        response = self.sock.recv(1024)
        if response == b'ok':
            data = self.sock.recv(1024)
            print(data.decode())
        else:
            print('文件库为空')


class FTPView:
    def __init__(self):
        self.client = FTPClient()

    def __display_menu(self):
        print("""
          ================  ftp 服务 ===============
          1.查看文件   2.下载文件   3.上传文件  4.退出
          =========================================
              """)

    def __select_menu(self):
        num = input('请输入选项:')
        if num == '1':
            self.client.view()
        elif num == '2':
            pass
        elif num == '3':
            pass
        elif num == '4':
            self.client.exit()
        else:
            print('请输入正确选项')

    def main(self):
        while True:
            self.__display_menu()
            self.__select_menu()


if __name__ == '__main__':
    ftp = FTPView()
    ftp.main()
